<?php
/*
 * @Desc: 
 * @Author: 小超越
 * @LastEditors: 小超越
 * @BlogUrl: www.zhe94.com
 */

namespace app\admin\business\article;


use think\facade\Request;
use app\admin\controller\article\Base;
use app\admin\model\article\Post as PostModel;
use app\admin\validate\article\Post as PostValidate;

class Post extends Base
{
    protected $postModel = '';
    public $msg='';//用于失败报错信息
    public function __construct()
    {
        $this->postModel = new PostModel();
    }
    /**
     * @description: 获取文章单条数据
     * @param $id 文章id 
     * @return array 
     */
    public function getPostFind($id){
      try {
        $result= PostModel::find($id);
      } catch (\Throwable $th) {
        $result=[];
      }
       return $result;
    }
    /**
     * @description: 获取文章列表数据
     * @param $wherdata 条件数组
     * @param $limit 获取条数
     * @return array 
     */
    public function getPostList($whereData = [], $limit)
    {
        $where = [];
        //更新时间
        $create_time_start = $whereData['create_time_start'] ?? '';
        $create_time_end = $whereData['create_time_end'] ?? '';
        //创建时间
        $update_time_start = $whereData['update_time_start'] ?? '';
        $update_time_end = $whereData['update_time_end'] ?? '';
        //按更新时间查找
        if ($create_time_start && $create_time_end) {
            $where[] = ["create_time", "between", [$create_time_start, date("Y-m-d", strtotime("$create_time_end +1 day"))]];
        }
        //按创建时间查找
        if ($update_time_start && $update_time_end) {
            $where[] = ["update_time", "between", [$update_time_start, date("Y-m-d", strtotime("$update_time_end +1 day"))]];
        }
        try {
            $list = $this->postModel->getDataByWhere($where, $limit);
            if(!$list){
                $list = ['items'=>[],'total'=>0,''];
            }
        } catch (\Exception $e) {
            $list = ['items'=>[],'total'=>0,''];
        }
        return $list;
    }
    /**
     * @description: 提交文章数据
     * @param {type} 
     * @return {type} 
     */
    public function setPostData($data)
    {
        //验证
        $validate = new PostValidate;
        if (!$validate->check($data)){
            $this->msg=$validate->getError();
            return [];
        }
        try {
           $post= PostModel::create($data);
        } catch (\Exception $e) {
            $this->msg=$e->getMessage();
            return [];
        }
        //返回主键ID
        return $post->id;
    }
    /**
     * @description: 文章数据更新
     * @param {type} 
     * @return {type} 
     */
    public function upPostData($data){
        //验证
        $validate = new PostValidate;
        if (!$validate->scene('edit')->check($data)){
            $this->msg=$validate->getError();
            return [];
        }
           
        try {
            $result=PostModel::update($data);
        } catch (\Exception $e) {
            $this->msg=$e->getMessage();
            return [];
        }
        return $result;
    }
    
}
